package com.example.event_resource_java.authority.controller.StatisticController;

import com.example.event_resource_java.authority.dao.ErrorBookDO;
import com.example.event_resource_java.authority.dao.PaperDao.ItemBankDO;
import com.example.event_resource_java.authority.server.ErrorBookService;
import com.example.event_resource_java.commons.annotation.Anonymous;
import com.example.event_resource_java.commons.enums.EnumErrorCode;
import com.example.event_resource_java.commons.util.ApiResult;
import com.example.event_resource_java.commons.vo.ErrorBookVO.ErrorBookSubmitVO;
import com.example.event_resource_java.commons.vo.ExaminationQuestionVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 错题本Controller
 */
@RestController
@RequestMapping("/errorBook")
public class ErrorBookController {
    @Autowired
    private ErrorBookService errorBookService;

    private final static Logger log = LoggerFactory.getLogger(ErrorBookController.class);

    // 错题教师统计查询
    @GetMapping("/teacherStatistic")
    @Anonymous
    public ApiResult teacherStatistic(ErrorBookDO errorBookDO) {
        ApiResult resultMsg;
        try {
            resultMsg = new ApiResult(EnumErrorCode.success.getCode(),
                    EnumErrorCode.success.getMsg(), errorBookService.teacherStatistic(errorBookDO));
        } catch (Exception e) {
            e.printStackTrace();
            log.error(String.format("错题集相关接口--系统错误,参数:%s,错误原因:%s", errorBookDO, e.getMessage()), e);
            return ApiResult.fail("请联系管理员");
        }
        return resultMsg;
    }

    // 错题学生统计查询
    @GetMapping("/userStatistic")
    @Anonymous
    public ApiResult userStatistic() {
        ApiResult resultMsg;
        try {
            resultMsg = new ApiResult(EnumErrorCode.success.getCode(),
                    EnumErrorCode.success.getMsg(), errorBookService.userStatistic());
        } catch (Exception e) {
            e.printStackTrace();
            log.error(String.format("错题集相关接口--系统错误,参数:%s,错误原因:%s", null, e.getMessage()), e);
            return ApiResult.fail("请联系管理员");
        }
        return resultMsg;
    }

    // 查看答案
    @GetMapping("/answer")
    public ApiResult answer(@RequestParam Integer topicType) {
        ApiResult resultMsg;
        try {
            resultMsg = new ApiResult(EnumErrorCode.success.getCode(),
                    EnumErrorCode.success.getMsg(), errorBookService.answer(topicType));
        } catch (Exception e) {
            e.printStackTrace();
            log.error(String.format("错题集相关接口--系统错误,参数:%s,错误原因:%s", topicType, e.getMessage()), e);
            return ApiResult.fail("请联系管理员");
        }
        return resultMsg;
    }

    // 错题练习
    @GetMapping("/practice")
    public ApiResult practice(Integer topicType) {
        ApiResult resultMsg;
        try {
            resultMsg = new ApiResult(EnumErrorCode.success.getCode(),
                    EnumErrorCode.success.getMsg(), errorBookService.practice(topicType));
        } catch (Exception e) {
            e.printStackTrace();
            log.error(String.format("错题集相关接口--系统错误,参数:%s,错误原因:%s", topicType, e.getMessage()), e);
            return ApiResult.fail("请联系管理员");
        }
        return resultMsg;
    }

    // 交卷
    @PostMapping("/submit")
    public ApiResult submit(@RequestBody ErrorBookSubmitVO errorBookSubmitVO) {
        ApiResult resultMsg;
        try {
            resultMsg = new ApiResult(EnumErrorCode.success.getCode(),
                    EnumErrorCode.success.getMsg(), errorBookService.submit(errorBookSubmitVO));
        } catch (Exception e) {
            e.printStackTrace();
            log.error(String.format("错题集相关接口--系统错误,参数:%s,错误原因:%s", errorBookSubmitVO, e.getMessage()), e);
            return ApiResult.fail("请联系管理员");
        }
        return resultMsg;
    }

}
